package com.kitbox.hl.meshow.common.utils.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: honglei
 * @Date: 2019/9/4 17:42
 * @Description:
 */
public class XmlDom4JUtil {
    /**
     * xml转map
     *
     * @param
     * @return
     * @throws IOException
     */
    public static Map<String, String> xml2Map(InputStream inputStream) throws IOException {
        Map<String, String> map = new HashMap<String, String>();
        SAXReader reader = new SAXReader();
        InputStream is = null;
        Document document = null;
        try {
            is = inputStream;
            document = reader.read(is);
            Element root = document.getRootElement();
            List<Element> list = root.elements();
            for (Element e : list) {
                map.put(e.getName(), e.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            is.close();
        }
        return map;
    }
}
